package com.tenement.controller.foreground.user;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.tenement.base.constants.SysmConstants.state;
import com.tenement.mybatis.entity.User;
import com.tenement.service.interfaces.IUserService;
import com.tenement.utils.ftp_images_server.FtpConfig;
import com.tenement.utils.ftp_images_server.FtpUtil;
import com.tenement.utils.ftp_images_server.UploadUtils;

/**
 * 用户信息修改
 * 
 * @author wk
 *
 */
@Controller
public class UserModController {

	private static final Log logger = LogFactory.getLog(UserModController.class);

	@Autowired
	private IUserService iUserService;

	/**
	 * 进入用户重置密码页面，步骤一
	 * 
	 * @return
	 */
	@RequestMapping(value = "user_resetpwd.action")
	public String userResetPwd() {
		logger.info("【-用户进入重置密码页面步骤一-】");
		return "redirect:view/foreground/user/user_resetpwd.html";
	}

	/**
	 * 进入用户重置密码页面，步骤二
	 * 
	 * @return
	 */
	@RequestMapping(value = "user_resetpwd2.action")
	public String userResetPwd2() {
		logger.info("【-用户进入重置密码页面步骤二-】");
		return "redirect:view/foreground/user/user_resetpwd2.html";
	}

	/**
	 * 进入用户重置密码页面，步骤三
	 * 
	 * @return
	 */
	@RequestMapping(value = "user_resetpwd3.action")
	public String userResetPwd3() {
		logger.info("【-用户进入重置密码页面步骤三-】");
		return "redirect:view/foreground/user/user_resetpwd3.html";
	}
	// uId file nickName

	/**
	 * 用户修改头像,用户名
	 * 
	 * @param file
	 * @param uId
	 * @param nickName
	 * @return
	 */
	@RequestMapping(value = "userModMsg.action")
	public @ResponseBody String userModMsg(MultipartFile file, @RequestParam("uId") String uId,
			@RequestParam("nickName") String nickName,HttpServletRequest request) {
		logger.info("【修改用户信息controller】" + file + " " + uId + " " + nickName);
		if(file==null){
			return savePhotos(uId, nickName,request);
		}else{
			return savePhotos(file, uId, nickName,request);	
		}
		
	}

	/**
	 * 
	 * @param file
	 * @param uId
	 * @param nickName
	 * @return
	 */
	public String savePhotos(MultipartFile file, String uId, String nickName,HttpServletRequest request) {
		
		FtpConfig ftpConfig = new FtpConfig();
		User user = new User();
		String oldName = file.getOriginalFilename();// 获取图片原来的名字
		String picNewName = UploadUtils.generateRandonFileName(oldName);// 通过工具类产生新图片名称，防止重名
		String picSavePath = UploadUtils.generateRandomDir(picNewName);// 通过工具类把图片目录分级
		user.setuId(Integer.parseInt(uId));
		user.setuNickname(nickName);
		user.setuPhotoUrl(picSavePath+ "/");
		user.setuPhotoName(picNewName);
		boolean b = iUserService.modifyUserMsg(user,request);
		if (b) {
			try {
				FtpUtil.pictureUploadByConfig(ftpConfig, picNewName, picSavePath, file.getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
			return state.Success;
		} else {
			return state.Failed;
		}
	}
	
	public String savePhotos(String uId, String nickName,HttpServletRequest request) {
		
		User user = new User();
		user.setuId(Integer.parseInt(uId));
		user.setuNickname(nickName);
		boolean b = iUserService.modifyUserMsg(user,request);
		if (b) {
			return state.Success;
		} else {
			return state.Failed;
		}
	}

}
